﻿using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
   static Guid gID;

    protected void Page_Load(object sender, EventArgs e)
   {
 

        if (!IsPostBack)
        {
            gID = System.Guid.NewGuid();

            if (Request.QueryString["action"] == "edit")
            {
                Session["action"] = "edit";
                Session["id"] = Request.QueryString["id"];


                DataAccess.Operate.DA_Album_Inherit Album_Inherit = new DataAccess.Operate.DA_Album_Inherit();
                DataSet ds = new DataSet();
                ds = Album_Inherit.GetAllBySoftID(Session["id"]);
                MyDataGrid.DataSource = ds.Tables[0];
                MyDataGrid.DataBind();
            }
            else
            {
                Session["action"] = null;
            }

            if (Session["action"]!=null)
            {
                btnSave.Text = "保存";
                lblTitle.Text = "修改软件信息";

                DataAccess.Operate.DA_SoftWare_Operate SoftWare_Operate = new DataAccess.Operate.DA_SoftWare_Operate();
                DataEntity.T_SoftWare SoftWare = (DataEntity.T_SoftWare)SoftWare_Operate.SelectOne(Session["id"]);
                txtSoftWare_Keywords.Text = SoftWare.SoftWare_Keywords.ToString();
                txtSoftWare_NetsiskAddress.Text = SoftWare.SoftWare_NetsiskAddress.ToString();
                txtSoftWare_Remarks.Text = SoftWare.SoftWare_Remarks.ToString();
                txtSoftWareName.Text = SoftWare.SoftWare_Name.ToString();
                txtSowftWare_WebsiteAddress.Text = SoftWare.SoftWare_WebsiteAddress.ToString();
                ddlSoftWare_IsOk.SelectedValue = SoftWare.SoftWare_IsOk.ToString();
                ddlSoftWare_IsSecure.SelectedValue = SoftWare.SoftWare_IsSecure.ToString();
                ddlSoftWare_Rating.SelectedValue = SoftWare.SoftWare_Rating.ToString();

            }
        }
    }


    protected void btnUpload_ServerClick(object sender, EventArgs e)
    {

        if (this.uploadPhoto.Value == "")
        {
            this.Page.ClientScript.RegisterStartupScript(typeof(Page), "", "<script>alert('图片不能为空！');</script>");
            return;
        }
        //获得图象并把图象转换为byte[]
        HttpPostedFile upPhoto = uploadPhoto.PostedFile;
        int upPhotoLength = upPhoto.ContentLength;
        byte[] PhotoArray = new Byte[upPhotoLength];
        Stream photoStream = upPhoto.InputStream;
        photoStream.Read(PhotoArray, 0, upPhotoLength);
        DataEntity.T_Album Album = new DataEntity.T_Album();
        DataAccess.Operate.DA_Album_Operate Album_Operate = new DataAccess.Operate.DA_Album_Operate();
        Album.Album_Content = PhotoArray;
        if (Session["id"] != null)
        {
            Album.Album_Software_ID = new Guid(Session["id"].ToString());
        }
        else
        {
            Album.Album_Software_ID = gID;
        }
        if (Album_Operate.Insert(Album))
        {
            this.Page.ClientScript.RegisterStartupScript(typeof(Page), "", "<script>alert('上传成功！');</script>");
            DataAccess.Operate.DA_Album_Inherit Album_Inherit = new DataAccess.Operate.DA_Album_Inherit();
            DataSet ds = new DataSet();
            if (Session["id"] != null)
            {
                ds = Album_Inherit.GetAllBySoftID(Session["id"].ToString());
            }
            else
                ds = Album_Inherit.GetAllBySoftID(gID);
            MyDataGrid.DataSource = ds.Tables[0];
            MyDataGrid.DataBind();
        }
        else
        {
            this.Page.ClientScript.RegisterStartupScript(typeof(Page), "", "<script>alert('上传失败！');</script>");
        }

    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        DataAccess.Operate.DA_SoftWare_Operate SoftWare_Operate = new DataAccess.Operate.DA_SoftWare_Operate();
        DataEntity.T_SoftWare SoftWare = new DataEntity.T_SoftWare();
        SoftWare.SoftWare_ID = gID;
        SoftWare.SoftWare_Name = txtSoftWareName.Text.Trim();
        SoftWare.SoftWare_Keywords = txtSoftWare_Keywords.Text.Trim();
        SoftWare.SoftWare_IsOk = Convert.ToInt32(ddlSoftWare_IsOk.SelectedValue.ToString());
        SoftWare.SoftWare_IsSecure = Convert.ToInt32(ddlSoftWare_IsSecure.SelectedValue.ToString());
        SoftWare.SoftWare_NetsiskAddress = txtSoftWare_NetsiskAddress.Text.Trim();
        SoftWare.SoftWare_WebsiteAddress = txtSowftWare_WebsiteAddress.Text.Trim();
        SoftWare.SoftWare_Remarks = txtSoftWare_Remarks.Text;
        SoftWare.SoftWare_Rating = Convert.ToInt32(ddlSoftWare_Rating.SelectedValue.ToString());
        if (Session["action"] !=null)
        {
            SoftWare.SoftWare_ID =new Guid(Session["id"].ToString());
            if (SoftWare_Operate.Update(SoftWare))
            {
                this.Page.ClientScript.RegisterStartupScript(typeof(Page), "", "<script>alert('修改成功！');</script>");
            }
            else
            {
                this.Page.ClientScript.RegisterStartupScript(typeof(Page), "", "<script>alert('修改失败！');</script>");
            }
        }
        else
        {
            if (SoftWare_Operate.Insert(SoftWare))
            {
                this.Page.ClientScript.RegisterStartupScript(typeof(Page), "", "<script>alert('添加成功！');</script>");
                Response.AddHeader("Refresh", "0"); 
            }
            else
            {
                this.Page.ClientScript.RegisterStartupScript(typeof(Page), "", "<script>alert('添加失败！');</script>");
            }
        }
    }
    protected void btnSaveWebSite_Click(object sender, EventArgs e)
    {
        DataAccess.Operate.DA_WebSite_Operate WebSite_Operate = new DataAccess.Operate.DA_WebSite_Operate();
        DataEntity.T_WebSite WebSite = new DataEntity.T_WebSite();
        WebSite.WebSite_Name = txtWebSite_Name.Text.Trim();
        WebSite.WebSite_Address = txtWebSite_Address.Text.Trim();
        WebSite.WebSite_Rating = Convert.ToInt32(ddlWebSite_Rating.SelectedValue.ToString());

        if (WebSite_Operate.Insert(WebSite))
        {
            this.Page.ClientScript.RegisterStartupScript(typeof(Page), "", "<script>alert('添加成功！');</script>");
        }
        else
        {
            this.Page.ClientScript.RegisterStartupScript(typeof(Page), "", "<script>alert('添加失败！');</script>");
        }
    }
    protected void btnSave_Business_Click(object sender, EventArgs e)
    {
        DataAccess.Operate.DA_BusinessTag_Operate BusinessTag_Operate = new DataAccess.Operate.DA_BusinessTag_Operate();
        DataEntity.T_BusinessTag BusinessTag = new DataEntity.T_BusinessTag();
        BusinessTag.BusinessTag_Name = txtBusinessTag_Name.Text.Trim();

        if (BusinessTag_Operate.Insert(BusinessTag))
        {
            this.Page.ClientScript.RegisterStartupScript(typeof(Page), "", "<script>alert('添加成功！');</script>");
        }
        else
        {
            this.Page.ClientScript.RegisterStartupScript(typeof(Page), "", "<script>alert('添加失败！');</script>");
        }
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        DataAccess.Operate.DA_SoftWare_Operate SoftWare_Operate = new DataAccess.Operate.DA_SoftWare_Operate();
        if (SoftWare_Operate.Delete(Session["id"]))
        {
            this.Page.ClientScript.RegisterStartupScript(typeof(Page), "", "<script>alert('删除成功！');</script>");
        }
        else
        {
            this.Page.ClientScript.RegisterStartupScript(typeof(Page), "", "<script>alert('删除失败！');</script>");
        }
    }

    protected void btnSaveImage_Click(object sender, EventArgs e)
    {
     
            SqlConnection conn = new SqlConnection();
            string id = hldId.Value;
            conn.ConnectionString = @"data source=.\SQLEXPRESS;initial catalog=DB_SoftWareManage;UID=sa;PWD=gz..000";

            string strSql = "select * from t_Album where Album_ID='" + id + "'";//这里假设获取id为5的图片
            SqlCommand cmd = new SqlCommand(strSql, conn);
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                byte[] bytes = (byte[])dr["Album_Content"];
                FileStream fs = new FileStream(@"d:\" + dr["Album_ID"] + ".jpg", FileMode.Create, FileAccess.Write);
                fs.Write(bytes, 0, bytes.Length);
                fs.Flush();
                fs.Close();
            }
            dr.Close();
            this.Page.ClientScript.RegisterStartupScript(typeof(Page), "", "<script>alert('" + hldId.Value + ".jpg 保存成功！');</script>");
    }

    public void SaveNetImage()
    {
        String url = "http://img04.taobaocdn.com/sns_album/i4/T1yAdWXgdGXXb1upjX.jpg";
        String fileName = url.Substring(url.LastIndexOf("/") + 1);
        String refer = url.Substring(0, url.LastIndexOf("/") + 1);
        System.Net.HttpWebRequest req = System.Net.HttpWebRequest.Create(url) as System.Net.HttpWebRequest;
        req.AllowAutoRedirect = true;
        req.Referer = refer;
        req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13";
        System.Net.HttpWebResponse res = req.GetResponse() as System.Net.HttpWebResponse;
        System.IO.Stream stream = res.GetResponseStream();
        System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
        byte[] buffer = new byte[32 * 1024];
        int bytes;
        while ((bytes = stream.Read(buffer, 0, buffer.Length)) > 0)
        {
            memoryStream.Write(buffer, 0, bytes);
        }
        byte[] images = memoryStream.GetBuffer();
        res.Close();
        memoryStream.Dispose();
    }
}
